package com.liucxu.zimuzu.View.Activity;

import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.liucxu.zimuzu.Base.BaseActivity;
import com.liucxu.zimuzu.Base.BaseFragment;
import com.liucxu.zimuzu.R;
import com.liucxu.zimuzu.Utils.ViewUtils;
import com.liucxu.zimuzu.View.Fragement.ArticalFragment;
import com.liucxu.zimuzu.View.Fragement.ChannelFragment;
import com.liucxu.zimuzu.View.Fragement.HotMovieTvFragment;
import com.liucxu.zimuzu.View.Fragement.UpdateFragment;

public class MainActivity extends BaseActivity {

    private BottomNavigationBar bottom_navigation_bar;
    private FrameLayout content_frame;
    private FragmentManager mFragmentManager;
    //电影以及电视热门Fragment
    BaseFragment mHotMovieTvFragment;
    BaseFragment mUpdateTodayFragment;
    BaseFragment currentFragment;
    BaseFragment mChannelFragment;
    BaseFragment mArticalFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void setupView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        bottom_navigation_bar = $(R.id.bottom_navigation_bar);
        bottom_navigation_bar.setMode(BottomNavigationBar.MODE_SHIFTING);
        bottom_navigation_bar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottom_navigation_bar.setBarBackgroundColor(android.R.color.darker_gray);
        bottom_navigation_bar.setActiveColor("#0f88eb")
                .setInActiveColor("#d4d4d4")
                .setBarBackgroundColor("#f2f4f7");
        content_frame = $(R.id.content_frame);
        mFragmentManager = getSupportFragmentManager();
        bottom_navigation_bar.addItem(new BottomNavigationItem(R.mipmap.hot, "热门"))
                .addItem(new BottomNavigationItem(R.mipmap.newest, "更新"))
                .addItem(new BottomNavigationItem(R.mipmap.channel, "频道"))
                .addItem(new BottomNavigationItem(R.mipmap.news, "资讯"))
                .initialise();
        bottom_navigation_bar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        mFragmentManager.beginTransaction().hide(currentFragment).show(mHotMovieTvFragment).commitAllowingStateLoss();
                        currentFragment = mHotMovieTvFragment;
                        break;
                    case 1:
                        if (mUpdateTodayFragment == null) {
                            mUpdateTodayFragment = ViewUtils.createFragment(UpdateFragment.class, true);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_frame, mUpdateTodayFragment).commitAllowingStateLoss();
                        } else {
                            if (!mUpdateTodayFragment.isVisible()) {
                                mFragmentManager.beginTransaction().hide(currentFragment).show(mUpdateTodayFragment).commitAllowingStateLoss();
                            }
                        }
                        currentFragment = mUpdateTodayFragment;
                        break;
                    case 2:
                        if (mChannelFragment == null) {
                            mChannelFragment = ViewUtils.createFragment(ChannelFragment.class, true);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_frame, mChannelFragment).commitAllowingStateLoss();
                        } else {
                            if (!mChannelFragment.isVisible()) {
                                mFragmentManager.beginTransaction().hide(currentFragment).show(mChannelFragment).commitAllowingStateLoss();
                            }
                        }
                        currentFragment = mChannelFragment;
                        break;
                    case 3:
                        if (mArticalFragment == null) {
                            mArticalFragment = ViewUtils.createFragment(ArticalFragment.class, true);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_frame, mArticalFragment).commitAllowingStateLoss();
                        } else {
                            if (!mArticalFragment.isVisible()) {
                                mFragmentManager.beginTransaction().hide(currentFragment).show(mArticalFragment).commitAllowingStateLoss();
                            }
                        }
                        currentFragment = mArticalFragment;
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    @Override
    public void setupData() {
        mHotMovieTvFragment = ViewUtils.createFragment(HotMovieTvFragment.class, true);
        mFragmentManager.beginTransaction().add(R.id.content_frame, mHotMovieTvFragment).commit();
        currentFragment = mHotMovieTvFragment;
    }
}
